import { get, post } from '@ajax'
import { message } from 'antd'
import {router} from 'umi'

export default {
  namespace: 'company',
  state: {
    title: '',
    companyId: '',   // 公司id
    processList: [],
    addProcessShow: false,
    financeList: [],
  },
  reducers: {
    save(state, {payload}) {
      return { ...state, ...payload }
    },
  },
  effects: {
    * onEdit({val}, {put}) {
      const {id, companyName}= val
      router.push(`/system/company/edit?id=${id}`)
      yield put({
        type: 'save',
        payload: {
          title: companyName
        }
      })
    },
    * getProcess(e, {put, select}) {
      const {companyId}=yield select(s => s['company'])
      const res=yield get('/companyProcess/queryProcessByCompany', { companyId })
      if (res.success) {
        yield put({
          type: 'save',
          payload: {
            processList: res.data
          }
        })
      }
    },
    * addProcess(e, {put}) {
      yield put({
        type: 'save',
        payload: {
          addProcessShow: true
        }
      })
    },
    * onProcessSave({val}, {put, select}) {
      const {companyId}=yield select(s => s['company'])
      const res=yield post('/companyProcess/add', {
        ...val,
        companyId
      })
      if (res.success) {
        message.success('操作成功')
        yield put({
          type: 'save',
          payload: {
            addProcessShow: false
          }
        })
        yield put({
          type: 'getProcess',
        })
        yield put({
          type: 'getFinance',
        })
      }
    },
    * onProcessDelete({id}, {put}) {
      const res=yield get('/companyProcess/remove', {id})
      if (res.success) {
        message.success('删除成功')
        yield put({
          type: 'getProcess',
        })
        yield put({
          type: 'getFinance',
        })
      }
    },

    * getFinance(e, {put, select}) {
      const {companyId}=yield select(s => s['company'])
      const res=yield get('/companyProcess/queryFinanceRateByCompany', { companyId })
      if (res.success) {
        yield put({
          type: 'save',
          payload: {
            financeList: res.data
          }
        })
      }
    },
    * onFinanceSave(e, {put, select}) {
      const {financeList}=yield select(s => s['company'])
      const res=yield post('/companyProcess/saveFinanceRate', [...financeList])
      if (res.success) {
        message.success('保存成功')
        yield put({
          type: 'getFinance'
        })
      }
    }
  },
}
